package com.heima.publishwmnews.task;

import com.heima.feign.wemedia.Wemedia;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.pojo.WmNews;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class PublishWmNewsTask {
    @Autowired
    private Wemedia wemedia;
    /**
     * 延迟文章发布
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public  void  publish(){
        //1.远程调用wemedia查询自媒体文章表中状态为8 并且 发布时间小于当前时间的文章
        ResponseResult<List<WmNews>> responseResult = wemedia.findEnableNews();
        if (responseResult.getCode()== AppHttpCodeEnum.SUCCESS.getCode()){
            //2.远程调用对这些文章进行发布
            List<WmNews> newsList = responseResult.getData();
            if (newsList!=null && !newsList.isEmpty()){
                for (WmNews wmNews : newsList) {
                    wemedia.publish(wmNews);
                }
            }
        }
    }
}
